using System;
using EPiServer.Common;
using EPiServer.Common.Visits;

namespace EPiAbstractions.Common.Visits
{
    public class VisitHandlerFacade : IVisitHandlerFacade
    {
        private static VisitHandlerFacade _instance;

        public VisitHandlerFacade()
        {
            VisitHandler.VisitAdded += OnVisitAdded;
            VisitHandler.VisitRemoved += OnVisitRemoved;
        }

        public static VisitHandlerFacade Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new VisitHandlerFacade();

                return _instance;
            }

            set { _instance = value; }
        }

        #region IVisitHandlerFacade Members

        public event EPiServerCommonEventHandler VisitAdded;
        public event EPiServerCommonEventHandler VisitRemoved;

        public virtual IVisit AddVisit(IVisit visit)
        {
            return VisitHandler.AddVisit(visit);
        }

        public virtual IVisit GetVisit(Int32 id)
        {
            return VisitHandler.GetVisit(id);
        }

        public virtual void RemoveVisit(IVisit visit)
        {
            VisitHandler.RemoveVisit(visit);
        }

        public virtual VisitCollection GetVisits(IVisitableEntity visitableEntity, Boolean uniqueVisitors, Boolean includeUnknownVisitors,
                                                 Int32 page, Int32 pageSize, TimeSpan explicitCacheExpiration, out Int32 totalItems,
                                                 VisitSortOrder[] sortOrder)
        {
            return VisitHandler.GetVisits(visitableEntity, uniqueVisitors, includeUnknownVisitors, page, pageSize, explicitCacheExpiration,
                                          out totalItems, sortOrder);
        }

        public virtual VisitCollection GetVisits(IVisitableEntity visitableEntity, Boolean uniqueVisitors, Boolean includeUnknownVisitors,
                                                 DateTime startDate, DateTime endDate, Int32 page, Int32 pageSize,
                                                 TimeSpan explicitCacheExpiration, out Int32 totalItems, VisitSortOrder[] sortOrder)
        {
            return VisitHandler.GetVisits(visitableEntity, uniqueVisitors, includeUnknownVisitors, startDate, endDate, page, pageSize,
                                          explicitCacheExpiration, out totalItems, sortOrder);
        }

        public virtual VisitCollection GetVisits(IVisitableEntity visitableEntity, Boolean uniqueVisitors, Int32 page, Int32 pageSize,
                                                 TimeSpan explicitCacheExpiration, out Int32 totalItems, VisitSortOrder[] sortOrder)
        {
            return VisitHandler.GetVisits(visitableEntity, uniqueVisitors, page, pageSize, explicitCacheExpiration, out totalItems,
                                          sortOrder);
        }

        public virtual VisitCollection GetVisits(IVisitableEntity visitableEntity, Boolean uniqueVisitors, DateTime startDate,
                                                 DateTime endDate, Int32 page, Int32 pageSize, TimeSpan explicitCacheExpiration,
                                                 out Int32 totalItems, VisitSortOrder[] sortOrder)
        {
            return VisitHandler.GetVisits(visitableEntity, uniqueVisitors, startDate, endDate, page, pageSize, explicitCacheExpiration,
                                          out totalItems, sortOrder);
        }

        public virtual VisitCollection GetVisits(IVisitableEntity visitableEntity, IAuthor visitor, DateTime startDate, DateTime endDate,
                                                 Int32 page, Int32 pageSize, TimeSpan explicitCacheExpiration, out Int32 totalItems,
                                                 VisitSortOrder[] sortOrder)
        {
            return VisitHandler.GetVisits(visitableEntity, visitor, startDate, endDate, page, pageSize, explicitCacheExpiration,
                                          out totalItems, sortOrder);
        }

        public virtual Int32 GetNumVisits(IVisitableEntity visitableEntity)
        {
            return VisitHandler.GetNumVisits(visitableEntity);
        }

        public virtual Int32 GetNumVisits(IVisitableEntity visitableEntity, DateTime startDate, DateTime endDate,
                                          TimeSpan explicitCacheExpiration)
        {
            return VisitHandler.GetNumVisits(visitableEntity, startDate, endDate, explicitCacheExpiration);
        }

        public virtual Int32 GetNumUniqueVisits(IVisitableEntity visitableEntity, DateTime startDate, DateTime endDate,
                                                TimeSpan explicitCacheExpiration)
        {
            return VisitHandler.GetNumUniqueVisits(visitableEntity, startDate, endDate, explicitCacheExpiration);
        }

        public virtual Int32 GetNumUniqueVisits(IVisitableEntity visitableEntity, TimeSpan explicitCacheExpiration)
        {
            return VisitHandler.GetNumUniqueVisits(visitableEntity, explicitCacheExpiration);
        }

        public virtual IVisit GetLastVisit(IVisitableEntity visitableEntity)
        {
            return VisitHandler.GetLastVisit(visitableEntity);
        }

        public virtual VisitableEntityCollection GetVisitedItems(Int32 page, Int32 pageSize, TimeSpan explicitCacheExpiration,
                                                                 out Int32 totalItems, VisitableEntitySortOrder[] sortOrder)
        {
            return VisitHandler.GetVisitedItems(page, pageSize, explicitCacheExpiration, out totalItems, sortOrder);
        }

        public virtual VisitableEntityCollection GetVisitedItems(Type type, Int32 page, Int32 pageSize, TimeSpan explicitCacheExpiration,
                                                                 out Int32 totalItems, VisitableEntitySortOrder[] sortOrder)
        {
            return VisitHandler.GetVisitedItems(type, page, pageSize, explicitCacheExpiration, out totalItems, sortOrder);
        }

        public virtual VisitableEntityCollection GetVisitedItems(IAuthor visitor, Type type, Int32 page, Int32 pageSize,
                                                                 TimeSpan explicitCacheExpiration, out Int32 totalItems,
                                                                 VisitableEntitySortOrder[] sortOrder)
        {
            return VisitHandler.GetVisitedItems(visitor, type, page, pageSize, explicitCacheExpiration, out totalItems, sortOrder);
        }

        #endregion

        public virtual void OnVisitAdded(String sender, EPiServerCommonEventArgs e)
        {
            if (VisitAdded != null)
                VisitAdded(sender, e);
        }

        public virtual void OnVisitRemoved(String sender, EPiServerCommonEventArgs e)
        {
            if (VisitRemoved != null)
                VisitRemoved(sender, e);
        }
    }
}